/**
 * @(#) SensorValue.java
 */

package pcsr.hwVirtualization;

/**
 * Value of a sensor managed by PCSR.
 * @author Paulo Fagundes
 *
 */
public class SensorValue
{
	private Object extendedData;
	
	private int referential;
	
	private double sensorValue;

	/**
	 * {@inheritDoc}
	 */
	public boolean equals(Object obj)
	{
		if(obj == null)
		{
			return false;
		}
		
		if(!(obj instanceof SensorValue) )
		{
			return false;
		}
		
		SensorValue svObj = (SensorValue)obj;		
		if(
				sensorValue != svObj.sensorValue ||
				referential != svObj.referential ||
				extendedData != svObj.extendedData
			)
		{
			return false;
		}
		
		return true;
	}
	
	/**
	 * Gets the extended data.
	 * @return The extendedData
	 */
	public Object GetExtendedData( ) {
		return extendedData;
	}

	/**
	 * Gets the real value of the sensor.
	 * <p><strong>Remarks:</strong> This method is for driver use only.</p> 
	 * @return The real value of the sensor.
	 */
	public double GetUnnormalizedValue() {
		return sensorValue * referential;
	}
	
	/**
	 * Gets the relative value of the sensor.
	 * @return The sensorValue
	 */
	public double GetValue( ) {
		return sensorValue;
	}

	/**
	 * Sets extended data.
	 * @param extendedData The extendedData to set
	 */
	public void SetExtendedData( Object extendedData ) {
		this.extendedData = extendedData;
	}

	/**
	 * Sets the absolute value of the sensor. 
	 * <p><strong>Remarks:</strong> This method is for driver use only.</p> 
	 * @param sensorValue The sensorValue to set
	 */
	public void SetUnnormalizedValue( double sensorValue ) {
		if(Math.abs(sensorValue) >= referential)
		{
			this.sensorValue = 1;
		}
		else
		{
			this.sensorValue = sensorValue / referential;
		}
	}

	/**
	 * Sets the relative value of the sensor.
	 * @param sensorValue The sensorValue to set 
	 */
	public void SetValue( double sensorValue ) {
		if(Math.abs(sensorValue) >= 1)
		{
			this.sensorValue = 1;
		}
		else
		{
			this.sensorValue = sensorValue;
		}
	}

	/**
	 * Gets the referential value.
	 * <p><strong>Remarks:</strong> This method is for driver use only.</p>
	 * @return the referencial
	 */
	int GetReferencial( ) {
		return referential;
	}
	
	/**
	 * Sets the referential value. This value is used to calculate relative values.
	 * <p><strong>Remarks:</strong> This method is for driver use only.</p> 
	 * @param referential the referential to set
	 */
	void SetReferential( int referential ) {
		this.referential = referential;
	}
	
	/**
	 * SensorValue constructor.
	 * @param maxValue The maximum real value that sensor can read.
	 */
	public SensorValue(int maxValue)
	{
		referential = maxValue;
	}
}
